package com.security.antitheft.module;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationUpdateBinder {

	public static final int MIN_TIME_REQUEST = 15 * 1000 * 60;//15 min
	private Location currentLocation;
	private Location prevLocation;
	private Context context;
	private static LocationManager locationManager;
	private boolean isGpsEnabled, isNwEnabled;
	public LocationUpdateBinder(Context context) {
		this.context = context;
	}

	private void gotLocation(Location location) {
		
		prevLocation = currentLocation == null ?
				null : new Location(currentLocation);
		currentLocation = location;

		if (isLocationNew()) {
			//save to queue
			QueueHelper queue = new QueueHelper(context, true);
			queue.addLocation(location.getTime(), location.getAccuracy(), location.getBearing(), location.getSpeed(), 
					location.getAltitude(), location.getLatitude(), location.getLongitude(), isGpsEnabled ? 1:0);
			queue.close();
			queue = null;
		}
	}
	private boolean isLocationNew() {
		if (currentLocation == null) {
			return false;
		} else if (prevLocation == null) {
			return true;
		} else if (currentLocation.getTime() == prevLocation.getTime()) {
			return false;
		} else {
			return true;
		}
	}
	
	public void bind() {
		
		locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		
		isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		isNwEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_REQUEST,
					500, gpsListener);
		try {
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_REQUEST,
					500, nwListener);
		} catch (Exception e) {
			
		}
		
		if (isGpsEnabled) {
			Location gotLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			gotLocation(gotLoc);
		} else if (isNwEnabled) {
			Location gotLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
			gotLocation(gotLoc);
		}
	}
	
	
	public void unbind() {
		locationManager.removeUpdates(gpsListener);
		locationManager.removeUpdates(nwListener);
	}
	
	private LocationListener gpsListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			isGpsEnabled = true;
			gotLocation(locationManager.getLastKnownLocation(provider));
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			isGpsEnabled = false;
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			gotLocation(location);
			
		}
	};
	
	private LocationListener nwListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			isNwEnabled = true;
			if (!isGpsEnabled) 
				gotLocation(locationManager.getLastKnownLocation(provider));
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			isNwEnabled = false;
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			if (!isGpsEnabled)
				gotLocation(location);
		}
	};
}